package com.cc.openeye.dynamic.kuikly.recommend.items.dynamic

import com.cc.openeye.dynamic.recommend.entity.dynamicResponse.Data
import com.tencent.kuikly.core.base.ComposeView
import com.tencent.kuikly.core.base.ComposeAttr
import com.tencent.kuikly.core.base.ComposeEvent
import com.tencent.kuikly.core.base.ViewBuilder
import com.tencent.kuikly.core.base.ViewContainer
import com.tencent.kuikly.core.views.Image
import com.tencent.kuikly.core.views.Text

internal class InternalDynamicItemView : ComposeView<DynamicItemViewAttr, DynamicItemViewEvent>() {

    var height = 0f

    override fun created() {
        super.created()
        height = attr.data?.getHeight((attr.listWidth - 10) / 2) ?: 0f
    }

    override fun createEvent(): DynamicItemViewEvent {
        return DynamicItemViewEvent()
    }

    override fun createAttr(): DynamicItemViewAttr {
        return DynamicItemViewAttr()
    }

    override fun viewDidLayout() {
        super.viewDidLayout()
        event.onPreLoad?.invoke()
    }

    override fun body(): ViewBuilder {
        val ctx = this
        return {
            Image {
                attr {
                    height(ctx.height)
                    src(ctx.attr.data?.getImage()?.also {
                        println("====>image:${it}")
                    } ?: "")
                    borderRadius(8f)
                }
            }

            Text {
                attr {
                    text(ctx.attr.data?.content?.data?.description ?: "")
                    marginTop(5f)
                    fontSize(13f)
                }
            }
        }
    }
}


internal class DynamicItemViewAttr : ComposeAttr() {
    var data: Data? = null
    var isNeedPreLoad: Boolean = false
    var listWidth = 0f
}

internal class DynamicItemViewEvent : ComposeEvent() {
    var onPreLoad: (() -> Unit)? = null
}

internal fun ViewContainer<*, *>.DynamicItemView(init: InternalDynamicItemView.() -> Unit) {
    addChild(InternalDynamicItemView(), init)
}